package com.app.service.impl;

import com.app.common.Result;
import com.app.mapper.IntegralRecordMapper;
import com.app.mapper.UserMapper;
import com.app.model.IntegralRecordModel;
import com.app.model.UserModel;
import com.app.service.IntegralRecordService;
import com.app.vo.IntegralRecordVo;
import com.app.vo.RankingVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 积分使用记录
 *
 * @author 16693103420
 * @since 2024-01-25
 */
@Service
public class IntegralRecordServiceImpl extends ServiceImpl<IntegralRecordMapper, IntegralRecordModel> implements IntegralRecordService {
    @Autowired
    private IntegralRecordMapper integralRecordMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public IntegralRecordVo getUserIntegralRecordList(Integer page, Integer limit, Integer userId) {
        Page<IntegralRecordModel> page1 = new Page<>(page, limit);

        QueryWrapper<IntegralRecordModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.orderByDesc("create_time");
        Page<IntegralRecordModel> integralRecordPage = integralRecordMapper.selectPage(page1, qw);
        List<IntegralRecordModel> integralRecordList = integralRecordPage.getRecords();

        long count = integralRecordPage.getTotal();
        IntegralRecordVo integralRecordVo = new IntegralRecordVo();
        integralRecordVo.setList(integralRecordList);
        integralRecordVo.setCount((int) count);

        UserModel userModel = userMapper.selectById(userId);

        integralRecordVo.setIntegral(userModel.getIntegral());
        return integralRecordVo;
    }

    /**
     * 积分的排行榜
     * @return
     */
    @Override
    public Result integralRanking() {
        List<RankingVo> list = integralRecordMapper.integralRanking();
        return Result.success(list);
    }
}
